import { computed, ref } from 'vue';
import { defineStore } from 'pinia';
import { useDeviceStore } from '@/stores/device-store';
export const useEquipmentInfoStore = defineStore('equipmentInfo', () => {
    const equipmentInfo = ref(new Map());
    const ecgInfo = ref(new Map());
    const respInfo = ref(new Map());
    const spo2Info = ref(new Map());
    const valueDataInfo = ref(new Map());
    const nibpReviewInfo = ref(new Map());
    const currentId = ref();
    const deviceStore = useDeviceStore();
    const personLineData = computed(() => {
        return (type, device_Id) => {
            switch (type) {
                case 'ecg':
                    return ecgInfo.value.get(device_Id) || [];
                case 'spo2':
                    return spo2Info.value.get(device_Id) || [];
                case 'resp':
                    return respInfo.value.get(device_Id) || [];
                default:
                    return [];
            }
        };
    });
    const personValueData = computed(() => {
        return (device_Id) => {
            return valueDataInfo.value.get(device_Id) || {};
        };
    });
    const personNibpReview = computed(() => {
        return (device_Id) => {
            return nibpReviewInfo.value.get(device_Id) || {};
        };
    });
    const clearData = (deviceId) => {
        ecgInfo.value.set(deviceId, []);
        respInfo.value.set(deviceId, []);
        spo2Info.value.set(deviceId, []);
        equipmentInfo.value.set(deviceId, {});
        nibpReviewInfo.value.set(deviceId, {});
    };
    function updateData({ deviceId, waveData, valueData, nibpReview, firstTime }) {
        if (document.hidden) {
            clearData(deviceId);
            return;
        }
        if (waveData) {
            if (!ecgInfo.value.get(deviceId)) {
                ecgInfo.value.set(deviceId, []);
            }
            ecgInfo.value.get(deviceId).push(...waveData.ecg);
            // console.log(ecgInfo.value.get(deviceId))
            // console.log( document.hidden, 99999)
            if (!respInfo.value.get(deviceId)) {
                respInfo.value.set(deviceId, []);
            }
            respInfo.value.get(deviceId).push(...waveData.resp);
            if (!spo2Info.value.get(deviceId)) {
                spo2Info.value.set(deviceId, []);
            }
            spo2Info.value.get(deviceId).push(...waveData.spo2);
        }
        if (valueData) {
            valueDataInfo.value.set(deviceId, valueData);
        }
        if (nibpReview) {
            nibpReviewInfo.value.set(deviceId, nibpReview);
        }
        equipmentInfo.value.set(deviceId, {
            deviceId,
            firstTime
        });
    }
    function setEquipmentInfo(data) {
        const { deviceId, firstTime, nibpReview, valueData, waveData, } = data;
        if (!deviceId)
            return;
        // 进入详情时，不是当前设备的数据不接收，避免内存溢出
        if (currentId.value) {
            if (currentId.value === deviceId) {
                console.log(nibpReview);
                updateData({ deviceId, waveData, valueData, nibpReview, firstTime });
            }
        }
        else {
            updateData({ deviceId, waveData, valueData, nibpReview, firstTime });
        }
    }
    function shiftData(type, device_Id, num) {
        switch (type) {
            case 'ecg':
                ecgInfo.value.get(device_Id).splice(0, num);
                break;
            case 'spo2':
                spo2Info.value.get(device_Id).splice(0, num);
                break;
            case 'resp':
                respInfo.value.get(device_Id).splice(0, num);
                break;
            default:
                break;
        }
    }
    function setCurrentId(id) {
        currentId.value = id;
        // 进入详情时，将其他设备id的数据清空
        if (id) {
            deviceStore.deviceIds.forEach((key) => {
                if (key !== id) {
                    ecgInfo.value.set(key, []);
                }
            });
        }
        // console.log(ecgInfo.value)
    }
    return {
        setEquipmentInfo,
        personLineData,
        shiftData,
        personValueData,
        personNibpReview,
        setCurrentId
    };
});
